﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GuavaData;

namespace MultiClassDBA.ChannelBuilder
{
    public partial class TableEditor : Form
    {
        private List<Schema> schemaHistory;
        private String tableName;
        private Scheme s;
        private Form parentForm;

        public TableEditor(List<Schema> sh, String tableName, Form parentForm)
        {
            this.parentForm = parentForm;
            this.tableName = tableName;
            this.schemaHistory = sh;
            InitializeComponent();

            //Populate the fields
            tableNameTextBox.Text = tableName;
            Schema cs = schemaHistory[0];
            s = cs.getScheme(tableName);
            foreach (Column c in s.columns)
            {
                if(s.pkColumns.Contains(c))
                    pkCheckedListBox.Items.Add(c.Name, CheckState.Checked);
                else
                    pkCheckedListBox.Items.Add(c.Name, CheckState.Unchecked);
                if(s.fkColumns.Contains(c))
                    fkCheckedListBox.Items.Add(c.Name, CheckState.Checked);
                else
                    fkCheckedListBox.Items.Add(c.Name, CheckState.Unchecked);
            }
            foreach (Scheme sch in cs.tables)
            {
                if(s.parents.Contains(sch))
                    parentTablesCheckedListBox.Items.Add(sch.tableName, CheckState.Checked);
                else
                    parentTablesCheckedListBox.Items.Add(sch.tableName, CheckState.Unchecked);
                if(s.children.Contains(sch.TableName))
                    childTablesCheckedListBox.Items.Add(sch.tableName, CheckState.Checked);
                else
                    childTablesCheckedListBox.Items.Add(sch.tableName, CheckState.Unchecked);
            }
        }

        private void columnEditorButton_Click(object sender, EventArgs e)
        {
            new ColumnEditor(s).ShowDialog();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            //apply any changes
            s.pkColumns.Clear();
            foreach (String str in pkCheckedListBox.CheckedItems)
                s.pkColumns.Add(s.getColumn(str));
            s.fkColumns.Clear();
            foreach (String str in fkCheckedListBox.CheckedItems)
                s.fkColumns.Add(s.getColumn(str));
            s.parents.Clear();
            foreach (String str in parentTablesCheckedListBox.CheckedItems)
            {
                Scheme parent = schemaHistory[0].getScheme(str);
                parent.children.Add(tableName);
                s.parents.Add(parent);
            }
            s.children.Clear();
            foreach (String str in childTablesCheckedListBox.CheckedItems)
            {
                schemaHistory[0].getScheme(str).parents.Add(s);
                s.children.Add(str);
            }
            ((ChannelBuilder.ChannelBuilderMainWindow)this.parentForm).displayCurrentSchema();

            this.Close();
        }


    }
}
